<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="公众号名称" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="APPID" prop="appId" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.appId" placeholder="请输入APPID"></el-input>
      </el-form-item>
      <el-form-item label="APPSECRET" prop="appSecret" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.appSecret" type="password" placeholder="请输入APPSERCRET"></el-input>
      </el-form-item>
      <el-form-item label="SIGNTOKEN" prop="signToken">
        <el-input v-model="dataForm.email" placeholder="公众号管理后台token"></el-input>
      </el-form-item>
      <el-form-item label="AccessTokenUrl" prop="tokenUrl">
        <el-input v-model="dataForm.mobile" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="JsConfigUrl" prop="jsconfigUrl">
        <el-input v-model="dataForm.mobile" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio label="media_public">公众号</el-radio>
          <el-radio label="mini_program">小程序</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--<el-form-item label="状态" size="mini" prop="status">-->
      <!--<el-radio-group v-model="dataForm.status">-->
      <!--<el-radio :label="0">禁用</el-radio>-->
      <!--<el-radio :label="1">正常</el-radio>-->
      <!--</el-radio-group>-->
      <!--</el-form-item>-->
      <el-form-item label="LOGO" prop="logo">
        <el-input v-model="dataForm.logo" placeholder="logo地址，暂不提上传功能，可按需修改"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          appId: '',
          appSecret: '',
          name: '',
          signToken: '',
          tokenUrl: '',
          jsconfigUrl: '',
          type: 'media_public'
        },
        dataRule: {
          name: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          appSecret: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          appId: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.dataForm.appId = id || ''
        if (this.dataForm.appId) {
          this.$http({
            url: this.$http.adornUrl(`/wechat/mp/info/${this.dataForm.appId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            console.log(data)
            if (data && data.code === 1) {
              console.log(data.data)
              this.dataForm.appId = data.data.appId
              this.dataForm.appSecret = data.data.appSecret
              this.dataForm.name = data.data.name
              this.dataForm.tokenUrl = data.data.tokenUrl
              this.dataForm.jsconfigUrl = data.data.jsconfigUrl
              this.dataForm.signToken = data.data.signToken
              this.dataForm.logo = data.data.logo
              this.dataForm.type = data.data.type
            }
          })
        } else {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wechat/mp/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'appId': this.dataForm.appId || undefined,
                'appSecret': this.dataForm.appSecret,
                'name': this.dataForm.name,
                'tokenUrl': this.dataForm.tokenUrl,
                'jsconfigUrl': this.dataForm.jsconfigUrl,
                'signToken': this.dataForm.signToken,
                'logo': this.dataForm.logo,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
